package arch.coal.ellipse.security.dev;

import java.io.IOException;
import java.util.LinkedList;
import java.util.Vector;

import org.apache.log4j.Logger;

import arch.coal.ellipse.security.data.Item;
import arch.coal.ellipse.security.data.MSF020;
import arch.coal.ellipse.security.data.Position;
import arch.coal.ellipse.security.data.ProfilePositionAndValue;
import arch.coal.ellipse.security.utils.FileLoggerClass;




public class ProcessResultsOfMSF020 {
	
	private LinkedList summaryList;
	
	static Logger logger = Logger.getLogger(ProcessResultsOfMSF020.class);
	
	/**
	 * Method takes a LinkedList of MSF020 objects.  The method
	 * will go through the list and output the information in a 
	 * more usable form - primarily trying to note the specific
	 * position/value combination.
	 * @param list
	 */
	public void getProfileSummary(LinkedList list) {
		
		if( list == null || list.size() < 1 ) {
			return;
		}
		
		logger.debug("ENTERRING ...");
		summaryList = new LinkedList();
		int size = list.size();
		
		//Loop through LinkList passed in.  Each node contains an MSF020 data 
		//object.  For each MSF020 object extract relevant values in the .profile
		//field.
		for( int index=0; index<size; index++) {
			
			//get a node from the List and extract info.
			MSF020 msf020 = (MSF020) list.get(index);
			
			ProfilePositionAndValue pos = new ProfilePositionAndValue();
			pos.setEntry_type(msf020.getEntry_type().toString());
			pos.setEntity(msf020.getEntity());
			pos.setDistrict(msf020.getDstrct_code());
			pos.setProfile(msf020.getProfile());
			
			int profSize = msf020.getProfile().getNameParsedAsInt().length;
			Vector profPosVec = new Vector();
			
			Item tempItem = msf020.getProfile();			
			char [] charVal = tempItem.getNameParsed();
			logger.debug(tempItem.getNameParsed_toString());
			
			//for each node, examine the .profile field.
			for (int index2=0; index2<profSize; index2++) {
				
				char i = charVal[index2];
				
				if( i == '0' ||
					i == '1' ||
					i == '2' ||
					i == '3' ||
					i == '4' ||
					i == '5' ||
					i == '6' ||
					i == '7' ||
					i == '8' ||
					i == '9') {
					
						//If the profile (char[]) contains a numeric value
						//then record .profile position and value.
						Position p = new Position();
						p.setPosition( new Integer(index2).toString() );
						p.setValue(i);
						profPosVec.add(p);
				}//end of 'if'
				pos.setPosVec(profPosVec);
			}//end of inner 'for' looping through each .profile
			summaryList.add(pos);
		}//end of outer 'for'
	
	}

	/**
	 * Method returns a LinkedList of ProfilePositionAndValue objects.
	 * @return
	 */
	public LinkedList getSummaryList() {
		return summaryList;
	}
	
	/**
	 * Log the MSF020 object summary information.  
	 * @throws IOException
	 */
	public void printSummaryList() throws IOException {
		
		if ( summaryList == null || summaryList.size() < 1 ) {
			return; //nothing to print
		}
		
		FileLoggerClass fLog1 = new FileLoggerClass();
		for (int loop=0; loop<summaryList.size(); loop++){
			ProfilePositionAndValue p = (ProfilePositionAndValue) summaryList.get(loop);
			logger.debug(loop + "a: Entity[" + p.getEntity() 
					+ "] Entry_Type[" + p.getEntry_type() + "]" + p.getPosVecString());
			
			StringBuffer sb = new StringBuffer();
			sb.append(p.getEntity()).append(",").append(p.getEntry_type());
			sb.append(",").append(p.getDistrict()).append(",");
			sb.append(p.getPosVecString()).append("\n");
			fLog1.write(sb.toString());
		}
		logger.debug("Closing 1st logger");
		fLog1.close();
		
		//Print to 2nd log
		String lName = "C:\\JavaProgramInfo\\logs\\test1\\TEstLogChange.txt";
		FileLoggerClass fLog = new FileLoggerClass(lName);
		logger.debug("Starting new FileLogger");
		
		for (int loop=0; loop<summaryList.size(); loop++){
			ProfilePositionAndValue ppv = (ProfilePositionAndValue) summaryList.get(loop);
			logger.debug(loop + "b: Entity[" + ppv.getEntity() 
					+ "] Entry_Type[" + ppv.getEntry_type() + "]" + ppv.getPosVecString());
			
			StringBuffer sb2 = new StringBuffer();
			sb2.append(ppv.getEntity()).append("|").append(ppv.getEntry_type());
			sb2.append("|").append(ppv.getDistrict()).append("|");
			sb2.append(ppv.getProfile().getNameParsed_toString()).append("\n");
			fLog.write(sb2.toString());
		}
		fLog.close();
		logger.debug("Done writting to new FileLogger");
	}

}
